﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Thor.Common;
using Thor.Domain;
using Thor.Repository.EntFx;

namespace Thor.Repository.EntFx.Test
{
    /// <summary>
    /// Summary description for MessageRepositoryTest
    /// </summary>
    [TestClass]
    public class MessageRepositoryTest
    {
        #region generated code

        public MessageRepositoryTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #endregion

        #region tests

        [TestMethod]
        [Description("Thor.Repostiory.EntFx.MessageRepository")]
        public void InsertTest()
        {
            Model model = GetModel();
            int currentCount = model.QueueEntities.Count();

            MessageRepository repository = new MessageRepository(GetRepositoryParameters());

            Message message = new Message();
            message.Id = Guid.NewGuid().ToString();
            message.Text = "test message";

            repository.Insert(message);

            int expectedCount = currentCount + 1;
            int actualCount = model.QueueEntities.Count();

            Assert.AreEqual(expectedCount, actualCount);
        }

        [TestMethod]
        [Description("Thor.Repostiory.EntFx.MessageRepository")]
        public void FetchTest()
        {
            Model model = GetModel();
            int currentCount = model.QueueEntities.Count();

            MessageRepository repository = new MessageRepository(GetRepositoryParameters());

            var message = repository.Fetch();

            int expectedCount = currentCount - 1;
            int actualCount = model.QueueEntities.Count();

            Assert.AreEqual(expectedCount, actualCount);
        }

        [TestMethod]
        [Description("Thor.Repostiory.EntFx.MessageRepository")]
        public void PeekTest()
        {
            Model model = GetModel();
            int currentCount = model.QueueEntities.Count();

            MessageRepository repository = new MessageRepository(GetRepositoryParameters());

            var message = repository.Peek();

            int expectedCount = currentCount;
            int actualCount = model.QueueEntities.Count();

            Assert.AreEqual(expectedCount, actualCount);
        }

        [TestMethod]
        [Description("Thor.Repostiory.EntFx.MessageRepository")]
        public void DeleteTest()
        {
            MessageRepository repository = new MessageRepository(GetRepositoryParameters());

            Message message = new Message();
            message.Id = Guid.NewGuid().ToString();
            message.Text = "test message";

            repository.Insert(message);

            Model model = GetModel();
            int currentCount = model.QueueEntities.Count();

            repository.Delete(message);

            int expectedCount = currentCount - 1;
            int actualCount = model.QueueEntities.Count();

            Assert.AreEqual(expectedCount, actualCount);
        }

        [TestMethod]
        [Description("Thor.Repostiory.EntFx.MessageRepository")]
        public void ClearTest()
        {
            MessageRepository repository = new MessageRepository(GetRepositoryParameters());

            for (int i = 0; i < 10; i++)
            {
                Message message = new Message();
                message.Id = Guid.NewGuid().ToString();
                message.Text = "test message";
                repository.Insert(message);
            }

            repository.Clear();

            Model model = GetModel();

            int expectedCount = 0;
            int actualCount = model.QueueEntities.Count();

            Assert.AreEqual(expectedCount, actualCount);
        }

        #endregion

        #region helper methods

        private Model GetModel()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString;
            return new Model(connectionString);
        }

        private Dictionary<string, object> GetRepositoryParameters()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString;
            string applicationName = "Thor";

            Dictionary<string, object> parameters = new Dictionary<string, object>();

            parameters.Add("ApplicationName", applicationName);
            parameters.Add("ConnectionString", connectionString);

            return parameters;
        }

        #endregion
    }
}
